//
//  ViewController.m
//  简易通讯录
//
//  Created by apple on 15/8/26.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "Cities.h"
#import "Province.h"

@interface ViewController ()


{
    NSMutableArray *_contactArray;
//    NSMutableArray *provinceArray;
    
}
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *companyTextField;
@property (weak, nonatomic) IBOutlet UITextView *resultTextField;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;

@property (weak, nonatomic) IBOutlet UITextField *sexTextField;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *provinceArray = [NSMutableArray array];
    NSData *data = [NSData dataWithContentsOfFile:@"/Users/apple/Desktop/DistrictChuzhong.json"];
    
    if (data)
    {
        NSArray *l_array = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
//        快速枚举；
        [l_array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
            Province *province = [Province provinceFormDictionary:obj];
            [provinceArray addObject:province];
        
        }];
        
//        NSLog(@"%@",l_array);
       
//        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",str);
        
    }
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - 点击添加
- (IBAction)addButton:(UIButton *)sender {
    Contact *contact = [[Contact alloc] init];
    contact.name = _nameTextField.text;
    contact.sex = _sexTextField.text;
    contact.company = _companyTextField.text;
    NSLog(@"%@",_nameTextField.text);
    
    if (_contactArray == nil)
    {
        _contactArray = [NSMutableArray array];
    }
    [_contactArray addObject:contact];
    
    [self dispalyResult:_contactArray];
   //    使self.view上的所有编辑结束，收起键盘；
    [self.view endEditing:YES];
    
}
#pragma mark - 点击搜索
- (IBAction)searchButtion:(UIButton *)sender
{

//    创建一个谓词，用以对数组进行过滤；

#if 0
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@",_searchTextField.text];
    NSArray *array = [_contactArray filteredArrayUsingPredicate:predicate];
    
    [self dispalyResult:array];
#endif
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@"];//OR cities[name] CONTAINS %@
    NSArray *array1 = [provinceArray filteredArrayUsingPredicate:predicate];
    
    NSMutableString *allStr = [[NSMutableString alloc] init];

    for (Province *p in array1)
    {
        [allStr appendFormat:@"%@|%@|%@",p.name,p.id,p.cities];
    }

}

- (void)dispalyResult:(NSArray *)array
{
    
    NSMutableString *allStr = [[NSMutableString alloc] init];
    for (Contact *p in array)
    {
        //因为数组内的元素都是对象，我们需要将其转换成字符串的形式输出；
        [allStr appendFormat:@"%@|%@|%@\n",p.name,p.sex,p.company];
    }
    _resultTextField.text = allStr;
    

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
    // Dispose of any resources that can be recreated.
}

@end
